package org.nfIt.config;

import org.nfIt.interceptor.LoginAuthInterceptor;
import org.nfIt.properties.UserProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * ClossName:WebMvcConfiguration
 * Pakage:org.nfIt.config
 * Description
 *
 * 处理跨域问题
 *
 * @Author L
 * @Create 2024/6/18 16:23
 * @Version 2023.1.3
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    /**
     * 登入拦截器
     */
    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;

    /**
     * 注入拦截器优化的配置类
     */
    @Autowired
    private UserProperties userProperties;

    /**
     * 拦截器注册方法
     * @param registry 拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(loginAuthInterceptor)
                //excludePathPatterns() 那些路径不拦截
                //.excludePathPatterns("/admin/system/index/login","/admin/system/index/generateValidateCode")
                .excludePathPatterns(userProperties.getNoAuthUrls())
                //addPathPatterns() 那些路径需要拦截 ,/** 拦截所有路径
                .addPathPatterns("/**");
    }

    //处理跨域问题
    @Override
    public void addCorsMappings(CorsRegistry corsRegistry){

        corsRegistry.addMapping("/**")      // 添加路径规则
                .allowCredentials(true)               // 是否允许在跨域的情况下传递Cookie
                .allowedOriginPatterns("*")           // 允许请求来源的域规则
                .allowedMethods("*")
                .allowedHeaders("*") ;                // 允许所有的请求头
    }

}
